package ua.khpi.si.kit49.diploma.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Class for store weather
 * 
 * @author netstalk3r
 * 
 */
public class Weather implements Comparable<Weather> {

	private Double windSpeed;
	private String windDirection;
	private Integer temperature;
	private Date date;
	private String description;
	private List<Forecast> forecast;
	private SimpleDateFormat dateFormatter;

	/**
	 * Default constructor
	 */
	public Weather() {
		super();
		forecast = new ArrayList<Forecast>();
		dateFormatter = new SimpleDateFormat(Constants.DATE_FORMAT);
	}

	/**
	 * Constructor with parameters
	 * 
	 * @param windSpeed
	 * @param windDirection
	 * @param temperature
	 * @param date
	 * @param description
	 * @param forecast
	 */
	public Weather(Double windSpeed, String windDirection, Integer temperature,
			Date date, String description, List<Forecast> forecast) {
		super();
		this.windSpeed = windSpeed;
		this.windDirection = windDirection;
		this.temperature = temperature;
		this.date = date;
		this.description = description;
		this.forecast = forecast;
		dateFormatter = new SimpleDateFormat(Constants.DATE_FORMAT);
	}

	public Double getWindSpeed() {
		return windSpeed;
	}

	public void setWindSpeed(Double windSpeed) {
		this.windSpeed = windSpeed;
	}

	public String getWindDirection() {
		return windDirection;
	}

	public void setWindDirection(String windDirection) {
		this.windDirection = windDirection;
	}

	public Integer getTemperature() {
		return temperature;
	}

	public void setTemperature(Integer temperature) {
		this.temperature = temperature;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(String date) {
		if (this.date == null) {
			try {
				this.date = dateFormatter.parse(date);
			} catch (ParseException e) {
				this.date = new Date();
			}
		}
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Forecast> getForecast() {
		return forecast;
	}

	public void setForecast(List<Forecast> forecast) {
		this.forecast = forecast;
	}

	public void addForecast(Forecast forecast) {
		this.forecast.add(forecast);
	}

	@Override
	public int hashCode() {
		int hash = 11;
		hash = hash * 13 + ((windSpeed == null) ? 0 : windSpeed.hashCode());
		hash = hash * 13
				+ ((windDirection == null) ? 0 : windDirection.hashCode());
		hash = hash * 13 + ((temperature == null) ? 0 : temperature.hashCode());

		hash = hash * 13 + ((description == null) ? 0 : description.hashCode());
		hash = hash * 13 + ((forecast == null) ? 0 : forecast.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Weather))
			return false;
		Weather entry = (Weather) obj;
		return windSpeed.equals(entry.getWindSpeed())
				&& windDirection.equals(entry.getWindDirection())
				&& temperature.equals(entry.getTemperature())
				&& date.equals(entry.getDate())
				&& description.equals(entry.getDescription())
				&& forecast.equals(entry.getForecast());
	}

	@Override
	public int compareTo(Weather compareWeather) {
		double result = this.windSpeed - compareWeather.getWindSpeed();
		if (result != 0)
			return (int) (result / Math.abs(result));
		result = compareWeather.getTemperature() - this.temperature;
		return (int) (result / Math.abs(result));

	}
}
